package cc.vicp.djx314.starfood.web.user;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cc.vicp.djx314.starfood.domain.account.User;
import cc.vicp.djx314.starfood.service.account.UserInfoService;
import cc.vicp.djx314.starfood.service.exception.UserNotFoundException;
import cc.vicp.djx314.starfood.util.time.DateCanNullEditor;
import cc.vicp.djx314.starfood.web.exception.UserNotSameAsSessionException;
import cc.vicp.djx314.starfood.web.exception.UserSessionTimeOutException;
import cc.vicp.djx314.starfood.web.validator.UserUpdateValidator;

@Controller
public class UserInfoUpdateController {
	public static final Logger logger = LoggerFactory.getLogger(UserInfoUpdateController.class);

	@Autowired
	private UserInfoService userInfoService;

	@Autowired
	UserUpdateValidator userUpdateValidator;

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		//对于需要转换为Date类型的属性，使用DateEditor进行处理
		binder.registerCustomEditor(Date.class, new DateCanNullEditor());
	}

	@RequestMapping(value = "/user/update/{id}", method = RequestMethod.GET)
	public String userInfoUpdateData(@PathVariable("id") Integer id, Model model, HttpSession session)
			throws UserSessionTimeOutException, UserNotSameAsSessionException, UserNotFoundException {
		User user = (User) session.getAttribute("loginUser");
		if (user == null) {
			throw new UserSessionTimeOutException("用户session过时");
		}
		if (id.equals(user.getId()) == false) {
			throw new UserNotSameAsSessionException("请求的用户id:  " + id +
					"  与session的用户id:  " + user.getId() + "  不符");
		}
		User userInfo = userInfoService.getAllUserInfo(id);
		model.addAttribute("user", userInfo);
		return "customer/user/userinfoupdate";
	}

	@RequestMapping(value = "user/update/{id}", method = RequestMethod.PUT)
	public String userInfoUpdate(@PathVariable("id") Integer id,
			@ModelAttribute("user") @Valid User user, BindingResult result, HttpSession session)
					throws UserSessionTimeOutException, UserNotSameAsSessionException {
		User sessionUser = (User) session.getAttribute("loginUser");
		if (sessionUser == null) {
			throw new UserSessionTimeOutException("用户session过时");
		}
		if (id.equals(sessionUser.getId()) == false) {
			throw new UserNotSameAsSessionException("请求的用户id:  " + id +
					"  与session的用户id:  " + sessionUser.getId() + "  不符");
		}
		String resultString = null;
		user.setId(id);
		if (result.hasErrors()) {
			resultString = "customer/user/userinfoupdate";
		} else {
			userUpdateValidator.validate(user, result);
			if (result.hasErrors()) {
				resultString = "customer/user/userinfoupdate";
			} else {
				try {
					userInfoService.updateUserInfo(user);
					resultString = "redirect:/user/info/" + user.getId() + "?updateSuccess=true";
				} catch (Exception e) {
					logger.info("修改账户信息出错", e);
					resultString = "redirect:/user/update/" + id + "?unkonwnError=true";
				}
			}
		}
		return resultString;
	}
}